//
//  HRSoundExampleViewController.m
//  Hear
//
//  Created by mac on 2017/4/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSoundExampleViewController.h"
#import "MJRefresh.h"
#import "HRSoundExampleViewModel.h"
#import "HRSoundExampleModel.h"
#import "HRExampleSoundCollectionViewCell.h"
#import "Track.h"
#import "MTMusicFeature.h"
@interface HRSoundExampleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *contentArr;
@property (nonatomic, assign)NSInteger pIndex;
@property (nonatomic, strong)HRSoundExampleViewModel *viewModel;
@property (nonatomic, strong)UIView *headView;
@property (nonatomic, strong)HRSoundExampleModel *lastExampleModel;
@property (nonatomic, strong)NSIndexPath  *lastPlayIndexPath;

@end

@implementation HRSoundExampleViewController

-(UIView *)headView{
    if (_headView == nil){
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, kTopHeight, kFullWidth, 70)];
        UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
        iconImageView.image = [UIImage imageNamed:@"hearicon"];
        [_headView addSubview:iconImageView];
        iconImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        iconImageView.layer.shadowRadius      = 3;
        iconImageView.layer.shadowOpacity     = 0.2;
        iconImageView.layer.shadowOffset      = CGSizeMake(1, 1);
        iconImageView.layer.shadowPath        = [UIBezierPath bezierPathWithRect:iconImageView.bounds].CGPath;
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconImageView.right+20, iconImageView.top, kFullWidth-iconImageView.right-40, iconImageView.height)];
        contentLabel.text = @"如下是一些hearers的声音名片，点击试听";
        contentLabel.numberOfLines = 0;
        contentLabel.textColor = FirstTextColor;
        contentLabel.font = CustomUIFont(13);
        contentLabel.textAlignment = 1;
        [_headView addSubview:contentLabel];
        contentLabel.layer.borderColor = MainColor.CGColor;
        contentLabel.layer.cornerRadius = 5;
        contentLabel.layer.borderWidth = 1;
        
    }
    return _headView;
}

-(HRSoundExampleViewModel *)viewModel{
    if (_viewModel == nil){
        _viewModel = [HRSoundExampleViewModel new];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"声音名片";
    self.view.backgroundColor = [UIColor whiteColor];
    self.contentArr = [NSMutableArray array];
    
    [self setupView];
    [self getContentData];
}


- (void)setupView{
    
    [self.view addSubview:self.headView];
    
    CGFloat leftPading = 16;
    CGFloat cellW = (kFullWidth)/2;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(cellW, cellW);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.headView.bottom+leftPading, kFullWidth, kFullHeight-self.headView.bottom-leftPading) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[HRExampleSoundCollectionViewCell class] forCellWithReuseIdentifier:@"HRExampleSoundCollectionViewCell"];

}



#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.contentArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HRExampleSoundCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HRExampleSoundCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row%2 !=0){
        cell.backView.left = 0;
    }else{
        cell.backView.left = 10;
    }
    cell.model = self.contentArr[indexPath.row];
    WeakSelf
    cell.playExampleMedioBlock = ^(HRSoundExampleModel *currentExampleModel) {
        if (currentExampleModel || self.lastExampleModel){
            dispatch_async(dispatch_get_main_queue(), ^{
                if(![currentExampleModel.NickName isEqualToString:self.lastExampleModel.NickName]){
                    weakSelf.lastExampleModel.isPlaying = NO;
                }
                [weakSelf.collectionView reloadData];
                self.lastExampleModel = currentExampleModel;
            });
        }
    };
    return cell;
}

- (void)getContentData{
    self.pIndex = 0;
    
    [self getContentData:YES];
    WeakSelf
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pIndex = 0;
        [self getContentData:YES];
        [weakSelf.collectionView.mj_header endRefreshing];
    }];

    //    //上拉加载
    self.collectionView.mj_footer=[MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pIndex ++;
        [weakSelf getContentData:NO];
        [weakSelf.collectionView.mj_footer endRefreshing];
    }];
}

- (void)getContentData:(BOOL)isHead{
    WeakSelf
    [self.viewModel getSoundExampleConent:@{@"PageSize":@"8",@"PageIndex":[NSString stringWithFormat:@"%ld",(long)self.pIndex]} resulutArr:^(NSArray *resulutArr) {
        for (HRSoundExampleModel *model in resulutArr) {
            Track *track = [[Track alloc] init];
            track.audioFileURL = [NSURL URLWithString:model.SoundUrl];
            model.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
        }
        if (isHead){
            [weakSelf.contentArr removeAllObjects];
        }
        [weakSelf.contentArr addObjectsFromArray:resulutArr];
        [weakSelf.collectionView reloadData];
        if (resulutArr.count <= 8){
            weakSelf.collectionView.mj_footer.hidden = YES;
        }else{
            weakSelf.collectionView.mj_footer.hidden = NO;
        }
    }];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayVoiceNotification" object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
